<?php
/**
 * Created by PhpStorm.
 * User: liuyu
 * Date: 2018/11/19
 * Time: 11:19
 */

namespace app\api\controller\v7;

use app\api\controller\v2\Auth;
use app\common\model\Authenticate;
use app\common\model\WechatRecode;
use think\Cache;

/**
 * 鉴爱接口
 * Class IdentifyLove
 * @package app\api\controller\v7
 */
class IdentifyLove extends Auth
{

    /**
     * 鉴爱首页,获取当前用户的所有鉴爱记录
     * @method GET
     * @param int page 分页页码
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $page = input('param.page/d', 1);

        $data = (new Authenticate())->getAuthenticateHistory(['uid' => $this->user['id'], 'per_page' => 10, 'page' => $page]);

//        // 统计有多少订单
//        $total = count($data);
//        //获取数据分页信息
//        $per_page = 10;// 当前订单订单每页显示数量
//        $data_list = getPaginatorData($total, $page, $per_page);
//        //获取分页后的数据
//        $data_list['data'] = array_slice($data, $data_list['start'], $per_page);
//        unset($data_list['start']);
//        unset($data_list['end']);

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * 鑒愛歷史記錄刪除
     * @method DELETE
     * @param int rid1
     * @param int rid2
     * @param int wid1
     * @param int wid2
     * @return \think\response\Json
     */
    public function del_history()
    {
        if (!request()->isDelete())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $rid1 = input('param.rid1/d', 0);
        $wid1 = input('param.wid1/d', 0);
        $rid2 = input('param.rid2/d', 0);
        $wid2 = input('param.wid2/d', 0);

        $uid = $this->user['id'];

        //验证参数是否正确
        if ((!$rid1 && !$wid1) || ($rid1 && $wid1) || (!$rid2 && !$wid2) || ($rid2 && $wid2))
        {
            return show(config('code.error'), '操作错误');
        }

        $Authenticate = new Authenticate();

        if ($rid1 && $rid2)
        {
            $cacheKey = $uid . '_authlove_t1' . '_' . $rid1 . '_' . $rid2;
            try
            {
                $Authenticate->where("(rid1 = {$rid1} and rid2 = {$rid2}) OR (rid1 = {$rid2} and rid2 = {$rid1}) AND uid = {$uid}")
                    ->delete();
                Cache::rm('authenticate_history_' . $uid);
                Cache::rm($cacheKey);
            }
            catch (\Exception $e)
            {
                return show(config('code.error'), '操作错误');
            }

        } elseif (($rid1 && $wid2) || ($wid1 && $rid2))
        {
            $rid = $rid1 ? $rid1 : $rid2;
            $wid = $wid1 ? $wid1 : $wid2;
            $cacheKey = $uid . '_authlove_t2' . '_' . $rid . '_' . $wid;

            try
            {
                $Authenticate
                    ->where("(rid1 = {$rid} and wid2 = {$wid}) OR (wid1 = {$wid} and rid2 = {$rid}) AND uid = {$uid}")
                    ->delete();
                Cache::rm('authenticate_history_' . $uid);
                Cache::rm($cacheKey);
            }
            catch (\Exception $e)
            {
                return show(config('code.error'), '操作错误');
            }

        } elseif ($wid1 && $wid2)
        {
            $cacheKey = $uid . '_authlove_t3' . '_' . $wid1 . '_' . $wid2;
            try
            {
                $Authenticate
                    ->where("(wid1 = {$wid1} and wid2 = {$wid2}) OR (wid1 = {$wid2} and wid2 = {$wid1}) AND uid = {$uid}")
                    ->delete();
                Cache::rm('authenticate_history_' . $uid);
                Cache::rm($cacheKey);
            }
            catch (\Exception $e)
            {
                return show(config('code.error'), '操作错误');
            }
        } else
        {
            return show(config('code.error'), '操作错误');
        }
        return show(config('code.success'), '操作成功');
    }


    /**
     * 鉴爱结果
     * @method GET
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function result_info()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $rid1 = input('param.rid1/d', 0);
        $wid1 = input('param.wid1/d', 0);
        $rid2 = input('param.rid2/d', 0);
        $wid2 = input('param.wid2/d', 0);

        //验证参数是否正确
        if ((!$rid1 && !$wid1) || ($rid1 && $wid1) || (!$rid2 && !$wid2) || ($rid2 && $wid2))
        {
            return show(config('code.error'), '操作错误');
        }

        $map = [
            'uid' => $this->user['id'],
            'rid1' => $rid1,
            'wid1' => $wid1,
            'rid2' => $rid2,
            'wid2' => $wid2
        ];

        $data = (new Authenticate())->getAuthenticateResult($map);

        if (!empty($data) && isset($data['result_list1']) && isset($data['result_list2']))
        {
            $arr['archives1_info'] = $data['archives1_info'];
            $arr['archives2_info'] = $data['archives2_info'];
            $arr['result_list1'] = $data['result_list1'];
            $arr['result_list2'] = $data['result_list2'];
        } else
        {
            $arr = [];
        }

        return show(config('code.success'), '获取成功', $arr);
    }


    /**
     * 获取当前用户的所有微信档案信息
     * @method GET
     * @param int page default 1
     * @return \think\response\Json
     */
    public function wechat_recode()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $page = input('param.page/d', 1);

        $uid = $this->user['id'];

        $arr = (new WechatRecode())->getWechatRecodeLists(['uid' => $uid, 'page' => $page]);

        return show(config('code.success'), '获取成功', $arr);
    }

    /**
     * 删除微信档案
     * @method GET
     * @param int id default 0
     * @return \think\response\Json
     */
    public function del_wechat_recode()
    {
        if (!request()->isDelete())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $id = input('param.id/d', 0);
        $uid = $this->user['id'];

        if (!$id) return show(config('code.error'), '缺少参数id');

        try
        {
            (new WechatRecode())->where(['uid' => $uid, 'id' => $id])->update(['status' => 1]);

            Cache::rm('wechat_recode_lists_' . $uid);

            Cache::rm('wechat_recode_' . $id);

        }
        catch (\Exception $e)
        {
            return show(config('code.error'), '删除失败');
        }

        return show(config('code.success'), '删除成功');
    }
}
